package com.vma.manageSystem.controller;

import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import com.vma.manageSystem.common.GetPathByDate;
import com.vma.manageSystem.util.ReturnsUtil;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenzui on 2017/3/28.
 */
public class FileController extends Controller {
    /**
     * upload 图片上传方法
     * @throws IOException
     */
    public void upload() throws IOException {
        //根据时间生成文件夹目录
        UploadFile file = getFile();
        String imgName = file.getFileName();

        File source = new File(PathKit.getWebRootPath() + "/temp/" + imgName); // 获取临时文件对象
        String savePath = PathKit.getWebRootPath() + "/" + GetPathByDate.getPath();
        FileInputStream fis = new FileInputStream(source);
//
        File targetDir = new File(savePath);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        File target = new File(targetDir, imgName);
        if (!target.exists()) {
            target.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(target);
        byte[] bts = new byte[1024 * 20];
        while (fis.read(bts, 0, 1024 * 20) != -1) {
            fos.write(bts, 0, 1024 * 20);
        }

        fos.close();
        fis.close();
        renderJson(ReturnsUtil.returnSuccess( "/"+GetPathByDate.getPath()+"/"+imgName));
    }


    public void uploadImg() throws Exception {
        UploadFile uploadFile = getFile("uploadFile", PathKit.getWebRootPath()
                + "/temp", 20 * 1024 * 1024, "utf-8"); // 最大上传20M的图片
        // 异步上传时，无法通过uploadFile.getFileName()获取文件名
        String fileName = getPara("fileName");
        fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); // 去掉路径

        // 异步上传时，无法通过File source = uploadFile.getFile();获取文件
        File source = new File(PathKit.getWebRootPath() + "/temp/" + fileName); // 获取临时文件对象

        String extension = fileName.substring(fileName.lastIndexOf("."));
        String savePath = PathKit.getWebRootPath() + "/upload/images/"
                + GetPathByDate.getPath();
        FileInputStream fis = new FileInputStream(source);
//
        File targetDir = new File(savePath);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        File target = new File(targetDir, fileName);
        if (!target.exists()) {
            target.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(target);
        byte[] bts = new byte[1024 * 20];
        while (fis.read(bts, 0, 1024 * 20) != -1) {
            fos.write(bts, 0, 1024 * 20);
        }

        fos.close();
        fis.close();
//        JSONObject json = new JSONObject();
//
//        if (".png".equals(extension) || ".jpg".equals(extension)
//                || ".gif".equals(extension) || "jpeg".equals(extension)
//                || "bmp".equals(extension)) {
//            fileName = CommonUtils.getCurrentTime() + extension;
//
//            try {
//                FileInputStream fis = new FileInputStream(source);
//
//                File targetDir = new File(savePath);
//                if (!targetDir.exists()) {
//                    targetDir.mkdirs();
//                }
//
//                File target = new File(targetDir, fileName);
//                if (!target.exists()) {
//                    target.createNewFile();
//                }
//
//                FileOutputStream fos = new FileOutputStream(target);
//                byte[] bts = new byte[1024 * 20];
//                while (fis.read(bts, 0, 1024 * 20) != -1) {
//                    fos.write(bts, 0, 1024 * 20);
//                }
//
//                fos.close();
//                fis.close();
//                json.put("error", 0);
//                json.put("src", "upload/images/" + CommonUtils.getCurrentDate()
//                        + "/" + fileName); // 相对地址，显示图片用
//                source.delete();
//
//            } catch (FileNotFoundException e) {
//                json.put("error", 1);
//                json.put("message", "上传出现错误，请稍后再上传");
//            } catch (IOException e) {
//                json.put("error", 1);
//                json.put("message", "文件写入服务器出现错误，请稍后再上传");
//            }
//        } else {
//            source.delete();
//            json.put("error", 1);
//            json.put("message", "只允许上传png,jpg,jpeg,gif,bmp类型的图片文件");
//        }
//
//        renderJson(json.toJSONString());
    }
}
